package ihm;

import java.awt.EventQueue;
import java.awt.Font;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ScrollPaneConstants;
import javax.swing.border.EmptyBorder;

import metier.Entrainement;
import dao.DaoEntrainement;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

@SuppressWarnings("serial")
public class AccueilMoniteur extends JFrame implements ActionListener 
{

	private JPanel contentPane;
	private JMenuBar menuBar;
	private JMenu mnFichier;
	private JMenu mnConsulter;
	
	private JTable tableauConsult;
	private Object[][] lignes;
	private Vector<Entrainement> lesEntrainements = DaoEntrainement.getLesEntrainements();
	
	private JButton btnDeco;
	
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) 
	{
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try
				{
					AccueilMoniteur frame = new AccueilMoniteur();
					frame.setVisible(true);
				} catch (Exception e)
				{
					e.printStackTrace();
				}
			}
		});
	}
	
	//creation du tableau
	public void updateTableau()
	{
		String titres[]={"N° entrainement","date","heure de debut","heure de fin"};
		lignes=new Object[lesEntrainements.size()][4];
		int i = 0;
		for (Entrainement unEntr : this.lesEntrainements)
		{
			lignes[i][0] = unEntr.getNum();
	    	lignes[i][1] = unEntr.getDate();
	    	lignes[i][2] = unEntr.getHeureDeb();
	    	lignes[i][3] = unEntr.getHeureFin();

		   	i++;
		}
		this.tableauConsult = new JTable(lignes,titres);
	}
	
	/**
	 * Create the frame.
	 * @param numAdherent le numero de l'adhérent qui vient de se connecter 
	 */
	public AccueilMoniteur() 
	{
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		
		menuBar = new JMenuBar();
		setJMenuBar(menuBar);
		
		mnFichier = new JMenu("Fichier");
		menuBar.add(mnFichier);
		
		mnConsulter = new JMenu("Consulter");
		menuBar.add(mnConsulter);
		
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lblNom = new JLabel("Moniteur:");
		lblNom.setFont(new Font("Tahoma", Font.PLAIN, 13));
		lblNom.setBounds(10, 0, 63, 14);
		contentPane.add(lblNom);
		
		this.tableauConsult = new JTable();
		this.tableauConsult.setBounds(100, 400, 407, 188);
		
		//chargement du tableau
		updateTableau();
		JScrollPane scrollPane = new JScrollPane(tableauConsult,ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
		scrollPane.setBounds(20, 45, 404, 168);
		contentPane.add(scrollPane);
		
		btnDeco = new JButton("D\u00E9connexion");
		btnDeco.addActionListener(this);
		btnDeco.setBounds(312, -3, 122, 23);
		contentPane.add(btnDeco);
	}
	
	public void actionPerformed(ActionEvent evt) 
	{
		if(evt.getSource() == btnDeco)
		{
			this.setVisible(false);
			this.dispose();
			Connecter frameConnecter = new Connecter();
			frameConnecter.setVisible(true);
		}
	}
}
